package com.eascs.workflow.wf.utils;

import org.springframework.util.StringUtils;

import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFRequestParam;
import com.eascs.workflow.wf.vo.TransferNodeParam;

public class WFParamCheckUtils {
	/**
	 * 检查流程启动参数
	 * 
	 * @param param
	 * @return
	 */
	public static ModelResult<Boolean> checkWFRequestParam(WFRequestParam param) {
		if (StringUtils.isEmpty(param.getReferId())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.referid.isNull", "referid不能为空");
		}
		if (StringUtils.isEmpty(param.getReferCode())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.referCode.isNull", "referCode不能为空");
		}
		if (StringUtils.isEmpty(param.getReferUrl())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.referUrl.isNull", "referUrl不能为空");
		}
		if (StringUtils.isEmpty(param.getSystemModule())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.systemModule.isNull", "系统模块不能为空");
		}
		if (StringUtils.isEmpty(param.getProcessType())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.processType.isNull", "流程类型不能为空");
		}
		if (StringUtils.isEmpty(param.getAppliedby())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.appliedby.isNull", "申请人不能为空");
		}
		if (StringUtils.isEmpty(param.getAppliedbyName())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.appliedbyName.isNull", "申请人不能为空");
		}
		if (StringUtils.isEmpty(param.getOrgNo())) {
			return new ModelResult<Boolean>().withError("process.task.instance.param.orgNo.isNull", "经办平台不能为空");
		}
		return new ModelResult<Boolean>().withModel(Boolean.TRUE);
	}

	/**
	 * 检查流程流转参数
	 * 
	 * @param transferNodeParam
	 * @return
	 */
	public static ModelResult<Boolean> checkNodeTransferParam(TransferNodeParam param) {
		if (StringUtils.isEmpty(param.getNodeAuditorId())) {
			return new ModelResult<Boolean>().withError("process.task.node.transfer.param.NodeAuditorId.isNull", "NodeAuditorId不能为空");
		}
		if (StringUtils.isEmpty(param.getActionId())) {
			return new ModelResult<Boolean>().withError("process.task.node.transfer.param.actionId.isNull", "执行动作不能为空");
		}
		if (StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.trimToEmpty(param.getAuditNote()))) {
			return new ModelResult<Boolean>().withError("process.task.node.transfer.param.auditNote.isNull", "审核意见不能为空");
		}
		if (length(param.getAuditNote()) > 2000) {
			return new ModelResult<Boolean>().withError("字数超过限制,请以word附件形式上传或者精简文字", "字数超过限制,请以word附件形式上传或者精简文字");
		}
		if (param.getActionId() == TransferNodeParam.ACTION_SIGN) {
			if (StringUtils.isEmpty(param.getAdderId()) || StringUtils.isEmpty(param.getAdderName())) {
				return new ModelResult<Boolean>().withError("process.task.node.transfer.param.adder.isNull", "加签人不能为空");
			}
		}
		if (param.getActionId() == TransferNodeParam.ACTION_CHANGE) {
			if (StringUtils.isEmpty(param.getAdderId()) || StringUtils.isEmpty(param.getAdderName())) {
				return new ModelResult<Boolean>().withError("process.task.node.transfer.param.adder.isNull", "转移人不能为空");
			}
		}
		return new ModelResult<Boolean>().withModel(Boolean.TRUE);
	}

	/**
	 * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
	 * 
	 * @param String
	 *            s 需要得到长度的字符串
	 * @return int 得到的字符串长度
	 */
	public static int length(String s) {
		if (s == null)
			return 0;
		char[] c = s.toCharArray();
		int len = 0;
		for (int i = 0; i < c.length; i++) {
			len++;
			if (!isLetter(c[i])) {
				len++;
			}
		}
		return len;
	}

	public static boolean isLetter(char c) {
		int k = 0x80;
		return c / k == 0 ? true : false;
	}

	public static void main(String[] args) {
		System.out.println(length("我q110"));
	}

}
